<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Customlinks_model extends Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

   function list_customlinks($limit, $start, $customlinks_page_id='')
    {  // echo "limit".$limit."start".$start;
        try{
            $where="";
            if (!empty($customlinks_page_id)){
                $where=" where a.customlinks_page_id='{$customlinks_page_id}' ";
            }/*  else {
                $where=" where a.customlinks_page_id!=10 ";
            }*/
            $customlinks_sql = "select a.customlinks_id,
                    (select link_text from tbl_links b where b.id=a.customlinks_page_id) as customlinks_page_id ,
                    (select lvalue from tbl_lang b where b.lkey=a.title and b.lang='{$this->session->userdata("language")}' and module='customlinks') as title ,
                    (select lvalue from tbl_lang b where b.lkey=a.desc and b.lang='{$this->session->userdata("language")}' and module='customlinks') as `desc` ,
                    a.`status`
                    from tbl_customlinks a 
                    {$where}
                    order by customlinks_id, title
                          ";
           $customlinks_sql.= " limit ".$start.",".$limit;
           //echo $customlinks_sql;
            $customlinks = $this->db->query($customlinks_sql);
            if($customlinks->num_rows() > 0) {
                 return $customlinks_result = $customlinks->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
  function list_customlinks_count()
    {
        try{
            $customlinks_sql = "select  count(*) as count
                    from tbl_customlinks  
                          ";

            $customlinks = $this->db->query($customlinks_sql);
            if($customlinks->num_rows() > 0) {
                  $customlinks_result = $customlinks->row_array();
                  return $customlinks_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    function savecustomlinks($post_arr, $langarr)
    {
        $this->load->database();
    	$customlinksdetailsarray=array();
        $last_insert_id=-1;
        $customlinksdetailsarray["customlinks_page_id"]=$post_arr['customlinks_page_id'];        
        $customlinksdetailsarray["cost"]=$post_arr['cost'];
        $customlinksdetailsarray["status"]=$post_arr['status'];
        
        $sql = "INSERT INTO tbl_customlinks(customlinks_page_id, cost, status)
                VALUES('" . $customlinksdetailsarray["customlinks_page_id"] . "', '" . $customlinksdetailsarray["cost"] . "', '" . $customlinksdetailsarray["status"] . "')";
       $query = $this->db->query($sql);
    	if($query)
    	{
            $last_insert_id = $this->db->insert_id();
            $customlinks_title_lang_key = $last_insert_id . "_title";
            $customlinks_desc_lang_key = $last_insert_id . "_desc";

            $sql = "UPDATE tbl_customlinks SET title = '" . $customlinks_title_lang_key . "', `desc` = '" . $customlinks_desc_lang_key . "'
                    WHERE customlinks_id = " . $last_insert_id;
            $query = $this->db->query($sql);
            if($query)
            {
                foreach ($langarr as $langkey=>$langval)
                {
                    $customlinksdetailsarray['title'.$langkey]=$post_arr['title'.$langkey];
                    $customlinksdetailsarray['desc'.$langkey]=$post_arr['desc'.$langkey];
                    $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . $customlinks_title_lang_key . "', '" . $langkey . "', '" . $customlinksdetailsarray["title" . $langkey] . "', " . $last_insert_id . ", 'customlinks')";
                    $query = $this->db->query($sql);
                    if($query)
                    {
                        $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . $customlinks_desc_lang_key . "', '" . $langkey . "', '" . $customlinksdetailsarray["desc" . $langkey] . "', " . $last_insert_id . ", 'customlinks')";
                        $query = $this->db->query($sql);
                        
                    }
                }
            }
        }
        return $last_insert_id;
           
    }
    
    function get_customlinks_details_by_id($customlinks_id)
    {
        try {
            $customlinks_sql = "SELECT *
                FROM tbl_customlinks 
                WHERE customlinks_id = ?";
            $customlinks = $this->db->query($customlinks_sql, array($customlinks_id));
            if($customlinks->num_rows() > 0) {
                 return $customlinks_result = $customlinks->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    } 
    
    function get_customlinks_details_lang_by_id($lkey, $langkey, $module='customlinks')
    {
        $sql = "SELECT * FROM tbl_lang WHERE lkey = ? and lang=? and module=?";
        $facilty = $this->db->query($sql, array($lkey, $langkey, $module));
        if($facilty->num_rows() > 0) {
             return $fac_result = $facilty->result_array();
        } else {
            return FALSE;
        }
    }

    function get_hotel_list($hotelid)
    {
        if ($hotelid){
            $where=" and a.id={$hotelid} ";
        }
        $sql = "select *,a.id as hotel_id,
                    (select lvalue from tbl_lang b where b.lkey=a.hotelname and b.lang='{$this->session->userdata("language")}') as hotelname                     
                    from tbl_hotelmaster a where status=1 {$where} order by hotelname";
        $hotels = $this->db->query($sql);
        if($hotels->num_rows() > 0) {
             return $$hotel_result = $hotels->result_array();
        } else {
            return FALSE;
        }
    }

    function updatecustomlinks($post_arr, $data)
    {
        //print_r($post_arr['facility']);
        $sql = "UPDATE tbl_customlinks
                SET customlinks_page_id='".$post_arr['customlinks_page_id']."', cost ='".$post_arr['cost']."', status = '" . $post_arr['status'] . "'
                WHERE customlinks_id = " . $post_arr['customlinks_id'];
        
        $query = $this->db->query($sql);
        if($query)
        {
            foreach($data['partnerlang'] as $langkey=>$langval)
            {
                $sql_name = "UPDATE tbl_lang SET lvalue = '" . $post_arr['title' . $langkey] . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['customlinks_id'] . "
                                AND lkey = '" . $post_arr['customlinks_id'].'_title' . "'
                                    AND module = 'customlinks' ";
                
                $query_title = $this->db->query($sql_name);

                $sql_desc = "UPDATE tbl_lang SET lvalue = '" . $post_arr['desc' . $langkey] . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['customlinks_id'] . "
                                AND lkey='".$post_arr['customlinks_id'].'_desc'."'
                                    AND module = 'customlinks';";
                
                $query_desc = $this->db->query($sql_desc);
            }

            
        }
    }

    function delete_customlinks($customlinks_id)
    {
        $del_customlinks_sql = "DELETE FROM tbl_customlinks WHERE customlinks_id = " . $customlinks_id;
        $customlinks_qry = $this->db->query($del_customlinks_sql);
        if($customlinks_qry)
        {            
            $lang_sql = "DELETE FROM tbl_lang WHERE mod_id = " . $customlinks_id . " AND module='customlinks' ";
            $lang_qry = $this->db->query($lang_sql);            
        }
    }

    public function get_menulinks($link_type='', $menu_link=''){ 
        $where='';
        if(!empty($link_type)){
            $where=" and link_type='{$link_type}' ";
        }
        if(!empty($menu_link)){
            $where=" and id='{$menu_link}' ";
        }
        $menulinks_sql = "select id customlinks_page_id, link_text customlinks_page_name from tbl_links a  
                    where a.status=1  {$where}
                ";                   
        $menulinks = $this->db->query($menulinks_sql);
        
        if($menulinks->num_rows() > 0) {
            return $menulinks_result = $menulinks->result_array();
        }else{
            return FALSE;
        }
        
    }
    
    public function get_custom_list_id($customlinks_page_id=''){ 
        $where='';
        if(!empty($customlinks_page_id)){
            $where=" where customlinks_page_id='{$customlinks_page_id}' ";
        }else{
            return array(0=>array('customlinks_id'=>0));
        }
        $customlinks_sql = "select customlinks_id from tbl_customlinks a  
                    {$where}
                ";                   
        $customlinks_id = $this->db->query($customlinks_sql);
        
        if($customlinks_id->num_rows() > 0) {
            return $customlinks_id_result = $customlinks_id->result_array();
        }else{
            return array(0=>array('customlinks_id'=>0));
        }
        
    }
    
}
?>
